package com.qzxy.premiumhotelsystem.controller.guest;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.qzxy.premiumhotelsystem.common.annotation.LogExecutionTime;
import com.qzxy.premiumhotelsystem.common.constant.UserRoleConstant;
import com.qzxy.premiumhotelsystem.common.enums.LogLevel;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.entity.Comments;
import com.qzxy.premiumhotelsystem.domain.entity.Guests;
import com.qzxy.premiumhotelsystem.domain.vo.CommentVo;
import com.qzxy.premiumhotelsystem.service.CommentsService;
import com.qzxy.premiumhotelsystem.service.GuestsService;
import com.qzxy.premiumhotelsystem.service.LogsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CommentsController
 * Package: com.qzxy.premiumhotelsystem.controller
 * Description:
 *
 * @Author Qwh
 * @Create 2024/9/12 16:02
 * @Version 1.0
 */

@Tag(name = "评论接口")
@RestController("userCommentsController")
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;
    @Autowired
    private GuestsService guestsService;
    @Autowired
    private LogsService logsService;

    @GetMapping("{roomId}")
    @Operation(summary = "查看评论")
    public Result searchAllByRoomId(@PathVariable @NotBlank String roomId) {
        return Result.ok(commentsService.searchAllByRoomId(roomId));
    }

    @GetMapping("id")
    @Operation(summary = "查看评论各id")
    public Result searchAllCommentsByRoomId(@RequestParam @NotBlank String roomNumber) {
        return Result.ok(commentsService.searchAllCommentsByRoomId(roomNumber));
    }


    @GetMapping("all")
    @Operation(summary = "所有评论")
    public Result allComments() {
        return Result.ok(commentsService.selectAllComments());
    }

    @PostMapping("create")
    @Operation(summary = "新增评论")
    @LogExecutionTime(message = "新增评论")
    @SaCheckLogin
    @Transactional
    public Result createComment(@RequestBody Comments comments) {
        try {
            Guests guests = guestsService.getByTelephone(StpUtil.getLoginId().toString());
            comments.setGuestId(Integer.valueOf(guests.getGuestId()));
            return Result.ok(commentsService.create(comments));
        }catch (NotLoginException e){
            throw new RuntimeException("用户未登录，请先登录");
        }

    }

    @DeleteMapping("delete/{commentId}")
    @Operation(summary = "删除评论_评论id")
    @SaCheckRole(UserRoleConstant.ADMIN)
    @LogExecutionTime(message = "删除评论_评论id",level = LogLevel.WARN)
    public Result deleteCommentById(@PathVariable @NotNull String commentId) {
        return Result.ok(commentsService.removeById(commentId));
    }

    @GetMapping("commentTree")
    @Operation(summary = "多级评论（考虑是否使用）")
    public Result allCommentById(String roomNumber) {
        List<CommentVo> commentVos = commentsService.searchAllByRoomIdAndParentId(roomNumber);
        commentVos.forEach((commentVo)->{
            System.out.println("多级评论:"+commentVo);
        });
        commentVos.forEach((commentVo)->{
            commentVo.setReplies(commentsService.getCommentsHierarchy(String.valueOf(commentVo.getCommentId())));
        });
        return Result.ok(commentVos);
    }
//    @PostMapping("commentTree")
//    @Operation(summary = "多级评论（考虑是否使用）")
//    public Result allCommentById(String parentId) {
//        return Result.ok(commentsService.getCommentsHierarchy(parentId));
//    }


}
